var manageYearCtrl = myApp.controller('manageYearCtrl', ['$scope', '$rootScope' ,'NgTableParams', function($scope, $rootScope, NgTableParams) {
	$rootScope.firstType = Tools.permissionList[4].permissionId;
	$rootScope.secondType = Tools.permissionList[4].sonPermission[2].permissionId;
	
	$scope.newYearItem = {};
	$scope.modYearItem = {};
	$scope.delYearItem = {};

	// 提示弹层
	$scope.msg = "";
	$scope.visible = false;

	// 分页 
	$scope.pageList = [];
	$scope.curPage = 1;
	$scope.totalPage = 1;
	$scope.totalRow = 0;
	var PAGESIZE = 10;

	// 学年列表
	$scope.years = [];

	// 获取学年列表
	$scope.getYearList = function(curPage) {
		var data = {
			curPage: curPage,
			pagesize: PAGESIZE
		};
		commonSrv.getYearList(data, function(result) {
			var list = result.data;
			for(var i in list) {
				list[i].date = list[i].startDate + ' - ' + list[i].endDate;
				list[i].firstDate = list[i].firstStartDate + ' - ' + list[i].firstEndDate;
				list[i].secondDate = list[i].secondStartDate + ' - ' + list[i].secondEndDate;
			}
			$scope.yearList = new NgTableParams({count: PAGESIZE}, { counts: [],  dataset: list});
			var paginate = result.paginate;
			$scope.totalPage = paginate.totalPage;
			$scope.totalRow = paginate.totalRow;
			$scope.setPage(curPage);
		}, function(result) {
			if(result.status == 1) {
				$scope.msg = "获取学年列表失败";
				$scope.visible = true;
			}
		}, function() {
			$scope.$apply();
		})
	};
	// 设置分页
	$scope.setPage = function(curPage) {
		var newPageList = [];
		if (curPage < 1 || curPage > $scope.totalPage) {
			alert("设置分页失败");
			return;
		}
		var SHOWSIZE = 5;
		// 总页数小于SHOWSIZE
		if($scope.totalPage <= SHOWSIZE) {
			for(var i=0; i<$scope.totalPage; i++) {
				newPageList.push(i+1);
			}
		// 总页数大于等于SHOWSIZE
		} else {
			if(curPage > 2) {
				// 最后5页
				if(($scope.totalPage - curPage) < 2) {
					for(var i = ($scope.totalPage - 5); i < $scope.totalPage; i++) {
						newPageList.push(i + 1);
					}
				} else {
					for (var i = (curPage - 3); i < ((curPage + 2) > $scope.totalPage ? $scope.totalPage : (curPage + 2)); i++) {
                        newPageList.push(i + 1);
                    }
				}
			} else {
				for(var i=0; i<SHOWSIZE; i++) {
					newPageList.push(i + 1);
				}
			}
		}
        $scope.pageList = newPageList;
        $scope.curPage = curPage;
	};

	//设置当前选中页样式
    $scope.isActivePage = function (page) {
        return $scope.curPage == page;
    };
    //上一页
    $scope.previous = function () {
        if($scope.curPage > 1) {
        	$scope.getYearList($scope.curPage - 1);
        }
    }
    //下一页
    $scope.next = function () {
        if($scope.curPage < $scope.totalPage) {
        	$scope.getYearList($scope.curPage + 1);
        }
    };


    // 打开设置当前学年弹层
    $scope.openCurYear = function(item) {
    	$scope.modYearItem = item;
    	$('#setCurYearModal').modal({backdrop: 'static', keyboard: false, show: true});
    };
	// 设置当前学年
	$scope.setCurYear = function() {
		var data = {
			schoolyearId: $scope.modYearItem.schoolyearId
		};
		commonSrv.setCurYear(data, function(result) {
			if(result.status == 0) {
				$('#setCurYearModal').modal('hide');
				$scope.modYearItem = {};
				$scope.msg = "设置成功";
				$scope.visible = true;
				$scope.getYearList($scope.curPage);
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		})
	};
	// 初始化学年
	$scope.initYears = function() {
		var years = [];
		var year = new Date().getFullYear();
		console.log($scope.yearList.data)
		for(var i=year; i< (year+20); i++ ) {
			var j = i;
			years.push({id: i, label: i, name: i + '-' + (j+1)+ '学年'});
			if($scope.yearList.data.length > 0) {
				for(var k in $scope.yearList.data) {
					if($scope.yearList.data[k].year == j) {
						years.pop();
						break;
					}
				}
			}
		}
		$scope.years = years;
	};
	// 新增
	/**
	 * 打开新增学年弹层
	 */
	$scope.openAdd = function() {
		$scope.initYears();
		$scope.newYearItem = {
			selectYear: $scope.years[0],
			year: $scope.years[0].id,
			name: $scope.years[0].name 
		};
		$('#addYearModel').modal({backdrop: 'static', keyboard: false, show: true});
	};
	$scope.selectYear = function() {
		$scope.newYearItem.year = $scope.newYearItem.selectYear.id;
		$scope.newYearItem.name = $scope.newYearItem.selectYear.name;
	};
	/**
	 * 新增学年
	 */
	$scope.addYear = function() {
		var data = {
			year: $scope.newYearItem.year,
			name: $scope.newYearItem.name
		};
		commonSrv.addYear(data, function(result) {
			if(result.status == 0) {
				$scope.getYearList(1);
				$scope.msg = "新增成功";
				$scope.visible = true;
				$('#addYearModel').modal('hide');
				$scope.addYearModel = {};
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
	};

	// 删除
	/**
	 * 打开删除学年弹层
	 */
	$scope.openDel = function(item) {
		$scope.delYearItem = item;
		$('#delYearModel').modal('show');
	};
	/**
	 * 删除学年
	 */
	$scope.delYear = function() {
		var data = {
			schoolyearId: $scope.delYearItem.schoolyearId
		};
		commonSrv.delYear(data, function(result) {
			if(result.status == 0) {
				$scope.getYearList(1);
				$scope.msg = "删除成功";
				$scope.visible = true;
				$scope.getYearList($scope.curPage);
				$scope.delYearModel = {};
				$('#delYearModel').modal('hide');
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
	};

	// 修改
	/**
	 * 打开修改学年弹层
	 */
	$scope.openEdit = function(item) {
		$scope.modYearItem = angular.copy(item);
		$scope.modYearItem.warn = {
			name: "",
			dateTips: "",
			firstStartDate: "",
			firstEndDate: "",
			secondStartDate: "",
			secondEndDate: ""
		};
	
		// 转化时间
		var startDate = moment($scope.modYearItem.startDate).toDate();
		var endDate = moment($scope.modYearItem.endDate).toDate();
		var firstStartDate = moment($scope.modYearItem.firstStartDate).toDate();
		var firstEndDate = moment($scope.modYearItem.firstEndDate).toDate();
		var secondStartDate = moment($scope.modYearItem.secondStartDate).toDate();
		var secondEndDate = moment($scope.modYearItem.secondEndDate).toDate();

		// startDate
		$("#startDate").datepicker({
			format: "yyyy-mm-dd",
			language: 'zh-CN'
		});
		$("#startDate").datepicker("setDate", startDate);
		
		// endDate
		$("#endDate").datepicker({
			format: "yyyy-mm-dd",
			language: 'zh-CN'
		});
		$("#endDate").datepicker("setDate", endDate);
		
		// firstStartDate
		$("#firstStartDate").datepicker({
			format: "yyyy-mm-dd",
			language: 'zh-CN'
		});
		$("#firstStartDate").datepicker("setDate", firstStartDate);
		
		// firstEndDate
		$("#firstEndDate").datepicker({
			format: "yyyy-mm-dd",
			language: 'zh-CN'
		});
		$("#firstEndDate").datepicker("setDate", firstEndDate);
		
		// secondStartDate
		$("#secondStartDate").datepicker({
			format: "yyyy-mm-dd",
			language: 'zh-CN'
		});
		$("#secondStartDate").datepicker("setDate", secondStartDate);

		// secondEndDate
		$("#secondEndDate").datepicker({
			format: "yyyy-mm-dd",
			language: 'zh-CN'
		});
		$("#secondEndDate").datepicker("setDate", secondEndDate);

		$('#editYearModel').modal({backdrop: 'static', keyboard: false, show: true});
	};
	/**
	 * 修改学年
	 */
	$scope.editYear = function() {
		// 1.名称
		$scope.watchName();
		// 2.日期
		$scope.watchDate();
		// 判断是否可以提交
		if(!Tools.reg.isNull($scope.modYearItem.warn.name) || !Tools.reg.isNull($scope.modYearItem.warn.firstStartDate) || !Tools.reg.isNull($scope.modYearItem.warn.firstEndDate) || !Tools.reg.isNull($scope.modYearItem.warn.secondStartDate) || !Tools.reg.isNull($scope.modYearItem.warn.secondEndDate) || !Tools.reg.isNull($scope.modYearItem.warn.dateTips) ) {
			alert("请修改表单");
			return;
		}
		var data = {
			name: $scope.modYearItem.name,
			schoolyearId: $scope.modYearItem.schoolyearId,
			startDate: $scope.modYearItem.firstStartDate,
			endDate: $scope.modYearItem.secondEndDate,
			// startDate: $scope.modYearItem.startDate
			// endDate: $scope.modYearItem.endDate,
			firstStartDate: $scope.modYearItem.firstStartDate,
			firstEndDate: $scope.modYearItem.firstEndDate,
			secondStartDate: $scope.modYearItem.secondStartDate,
			secondEndDate: $scope.modYearItem.secondEndDate
		};
		commonSrv.editYear(data, function(result) {
			if(result.status == 0) {
				$scope.getYearList($scope.curPage);
				$scope.msg = "修改成功";
				$scope.visible = true;
				$('#editYearModel').modal('hide');
				$scope.modYearItem = {};
			}
		}, function(result) {
			if(result.status == 1) {
				alert(result.msg);
			}
		}, function() {
			$scope.$apply();
		});
	};
	// 打开日期插件
	$scope.openDate = function(target) {
		var $myDatePicker = $(target).parents('.input-group').children('.myDatePicker');
		$myDatePicker.datepicker('show');
	};
	// 清除日期值
	$scope.cleanDate = function(target) {
		var $myDatePicker = $(target).parents('.input-group').children('.myDatePicker');
		$myDatePicker.datepicker('update', '');
	};

	// 检验表单
	// 1.名称
	$scope.watchName = function() {
		var warn = "请输入长度为2-16个字符";

		if(Tools.reg.isNull($scope.modYearItem.name) || ($scope.modYearItem.name.length < 2 || $scope.modYearItem.name.length > 16)) {
			$scope.modYearItem.warn.name = warn;
		} else {
			$scope.modYearItem.warn.name = "";
		}
	};
	$scope.watchDate = function() {
		// firstStartDate
		if(Tools.reg.isNull($scope.modYearItem.firstStartDate)) {
			$scope.modYearItem.warn.firstStartDate = "请选择上学期开始日期";
		} else {
			$scope.modYearItem.warn.firstStartDate = "";
		}
		// firstEndDate
		if(Tools.reg.isNull($scope.modYearItem.firstEndDate)) {
			$scope.modYearItem.warn.firstEndDate = "请选择上学期结束日期";
		} else {
			$scope.modYearItem.warn.firstEndDate = "";
		}
		// secondStartDate
		if(Tools.reg.isNull($scope.modYearItem.secondStartDate)) {
			$scope.modYearItem.warn.secondStartDate = "请选择下学期开始日期";
		} else {
			$scope.modYearItem.warn.secondStartDate = "";
		}
		// secondEndDate
		if(Tools.reg.isNull($scope.modYearItem.secondEndDate)) {
			$scope.modYearItem.warn.secondEndDate = "请选择下学期结束日期";
		} else {
			$scope.modYearItem.warn.secondEndDate = "";
		}


		// 只判断firstStartDate, firstEndDate, secondStartDate, secondEndDate
		if($scope.modYearItem.firstStartDate < $scope.modYearItem.secondEndDate) {
			if($scope.modYearItem.firstStartDate < $scope.modYearItem.secondStartDate) {
				if ($scope.modYearItem.firstStartDate < $scope.modYearItem.firstEndDate) {
					if($scope.modYearItem.firstEndDate < $scope.modYearItem.secondEndDate) {
						if($scope.modYearItem.firstEndDate < $scope.modYearItem.secondStartDate) {
							if($scope.modYearItem.secondStartDate < $scope.modYearItem.secondEndDate) {
								$scope.modYearItem.warn.dateTips = "";
							} else {
								$scope.modYearItem.warn.dateTips = "（下学期开始日期需<strong>小于</strong>下学期结束日期）";
							}
						} else {
							$scope.modYearItem.warn.dateTips = "（上学期结束日期需<strong>小于</strong>下学期开始日期）";
						}
					} else {
						$scope.modYearItem.warn.dateTips = "（上学期结束日期需<strong>小于</strong>下学期结束日期）";
					}
				} else {
					$scope.modYearItem.warn.dateTips = "（上学期开始日期需<strong>小于</strong>上学期结束日期）"
				}
			} else {
				$scope.modYearItem.warn.dateTips = "（上学期开始日期需<strong>小于</strong>下学期开始日期）";
			}
		} else {
			$scope.modYearItem.warn.dateTips = "（上学期开始日期需<strong>小于</strong>下学期结束日期）";
		}
	}
	// 初始化
	function init() {
		// 设置年列表
		$scope.getYearList(1);
		$(document).ready(function() {
			
		});
	}
	init();
}]);